﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FdControlls.Fonts
{
    /// <summary>
    /// 自定义属性选择控件 
    /// 功能: 选择字体图标
    /// 结果: 返回对应图标的编码
    /// </summary>
    [ToolboxItem(false)]
    public partial class ScopeEditorControl : UserControl
    {

        public delegate void CLOSE();
        public CLOSE close;

        #region 私有变量定义
        /// <summary>
        /// 表格的列数
        /// </summary>
        private int dataGridViewRowCount = 10;
        /// <summary>
        /// 选择的位置 行*dataGridViewRowCount + 列
        /// </summary>
        private int selectIndex;
        /// <summary>
        /// 字体的编码集合
        /// </summary>
        private List<int> fontsCode;
        /// <summary>
        /// 老的字符编码
        /// </summary>
        private EIconType _oldScope;
        /// <summary>
        /// 新的字符编码
        /// </summary>
        private EIconType _newScope;
        /// <summary>
        /// 是否关闭了
        /// </summary>
        private Boolean canceling;
        #endregion

        #region 属性定义
        /// <summary>
        /// 新的编码 图标
        /// </summary>
        public EIconType Scope
        {
            get
            {
                return _newScope;
            }
        }
        #endregion

        #region 初始化 dataGridView 和字体图标编码集合
        /// <summary>
        /// 加载的时候初始图标信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void init()
        {
            fontsCode = new List<int>();
            foreach (EIconType item in Enum.GetValues(typeof(EIconType)))
            {
                fontsCode.Add((int)item);
            }
            if (fontsCode.Count < 1)
            {
                MessageBox.Show("请安装字体", "验证错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            _newScope = EIconType.ICON_100;
            FpDgv();
            SetDgw(fontsCode);
        }
        /// <summary>
        /// 设置 dataGridView 内容
        /// </summary>
        /// <param name="fontsCode"></param>
        private void SetDgw(List<int> fontsCode)
        {
            try
            {
                dataGridView1.Rows.Clear();

                for (int i = 0; i < fontsCode.Count; i += dataGridViewRowCount)
                {

                    int index = this.dataGridView1.Rows.Add();
                    for (int j = 0; j < 10; j++)
                    {
                        if (i + j >= fontsCode.Count) break;
                        this.dataGridView1.Rows[index].Cells[j].Value = char.ConvertFromUtf32(fontsCode[i + j]);
                    }

                    this.dataGridView1.Rows[index].Height = 26;

                    DataGridViewCellStyle ds = this.dataGridView1.Rows[index].DefaultCellStyle;

                    this.dataGridView1.Rows[index].DefaultCellStyle = ds;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("请安装字体");
            }
        }
        /// <summary>
        /// 设置 dataGridView 标题
        /// </summary>
        private void FpDgv()
        {
            try
            {
                dataGridView1.ColumnCount = dataGridViewRowCount;
                dataGridView1.ColumnHeadersVisible = true;
                dataGridView1.Font = new Font("Material Icons", 16, FontStyle.Regular);
                // Set the column header style.
                DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();

                columnHeaderStyle.BackColor = System.Drawing.Color.Beige;
                //      columnHeaderStyle.Font = new Font(, 10, FontStyle.Regular);

                dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    dataGridView1.Columns[i].Name = "";
                    dataGridView1.Columns[i].Width = 28;
                }
            } catch( Exception ex)
            {
                MessageBox.Show("请安装字体");
            }
           
        }
        #endregion

        #region 类初始
        /// <summary>
        /// 设置初始  并且赋值 编码
        /// </summary>
        /// <param name="scope"></param>
        public ScopeEditorControl(EIconType scope, CLOSE c)
        {
            _oldScope = scope;
            _newScope = scope;
            close = c;
            InitializeComponent();
        }
        /// <summary>
        /// 加载的时候初始图标信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScopeEditorControl_Load(object sender, EventArgs e)
        {
           init();
        }
        #endregion

        #region 事件响应函数 ESC按键 焦点离开窗口 选择图标
        /// <summary>
        /// 按 ESC 不变符号
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                _oldScope = _newScope;
                canceling = true;
            }
         
            return base.ProcessDialogKey(keyData);
        }
        /// <summary>
        /// 窗口关闭的时候 赋值选择的字符图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScopeEditorControl_Leave(object sender, EventArgs e)
        {
            if (!canceling)
            {
                _newScope = (EIconType)(fontsCode[selectIndex]);
            }
        }
      
        /// <summary>
        /// 选择图标的时候 保留选择的 行列和
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            selectIndex = e.RowIndex * dataGridViewRowCount + e.ColumnIndex;
            if (close != null)
            {
                close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }
        #endregion
    }
}
